package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;

import com.lanou3g.musicenjoy.adapter.SingerFragmentAdapter;
import com.lanou3g.musicenjoy.entity.SingerEntity;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class SingerFragment extends Fragment implements View.OnClickListener {
    private int[] lists = {R.id.chinaboy, R.id.chinagirl, R.id.china, R.id.occidentboy, R.id.occidentgirl,
            R.id.occident, R.id.koreaboy, R.id.koreagirl, R.id.korea, R.id.japanboy, R.id.japangirl, R.id.japan,
            R.id.other};
    private RecyclerView.Adapter adapter;
    private View view;
    private RequestQueue queue;
    private List<SingerEntity.ArtistEntity> datas;
    private RecyclerView recyclerView;
    private SingerActivityFragment singerActivityFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_singer, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //通过循环遍历进行初始化和监听
        for (int list : lists) {
            getActivity().findViewById(list).setOnClickListener(this);
        }
        queue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        recyclerView = (RecyclerView) view.findViewById(R.id.singerRecyclerView);
        recyclerView.setLayoutManager(null);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setOnClickListener(this);
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.artist.get72HotArtist&format=json=1&offset=0&limit=12";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    SingerEntity result = gson.fromJson(response, SingerEntity.class);
                    if (result != null) {
                        datas = result.getArtist();
                        adapter = new SingerFragmentAdapter(getActivity(), datas);
                        recyclerView.setAdapter(adapter);

                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chinaboy:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundle=new Bundle();
                bundle.putString("name", "华语男歌手");
                bundle.putInt("area", 6);
                bundle.putInt("sex", 1);
                singerActivityFragment.setArguments(bundle);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container,singerActivityFragment).commit();
                break;
            case R.id.chinagirl:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlechinagirl=new Bundle();
                bundlechinagirl.putString("name", "华语女歌手");
                bundlechinagirl.putInt("area", 6);
                bundlechinagirl.putInt("sex", 2);
                singerActivityFragment.setArguments(bundlechinagirl);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.china:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlechina=new Bundle();
                bundlechina.putString("name", "华语组合");
                bundlechina.putInt("area", 6);
                bundlechina.putInt("sex", 3);
                singerActivityFragment.setArguments(bundlechina);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.occidentboy:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundleoccidentboy=new Bundle();
                bundleoccidentboy.putString("name", "欧美男歌手");
                bundleoccidentboy.putInt("area", 3);
                bundleoccidentboy.putInt("sex", 1);
                singerActivityFragment.setArguments(bundleoccidentboy);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment)
                        .commit();
                break;
            case R.id.occidentgirl:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundleoccidentgirl=new Bundle();
                bundleoccidentgirl.putString("name", "欧美女歌手");
                bundleoccidentgirl.putInt("area", 3);
                bundleoccidentgirl.putInt("sex", 2);
                singerActivityFragment.setArguments(bundleoccidentgirl);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.occident:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundleoccident=new Bundle();
                bundleoccident.putString("name", "欧美组合");
                bundleoccident.putInt("area", 3);
                bundleoccident.putInt("sex", 3);
                singerActivityFragment.setArguments(bundleoccident);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.koreaboy:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlekoreaboy=new Bundle();
                bundlekoreaboy.putString("name", "韩国男歌手");
                bundlekoreaboy.putInt("area", 7);
                bundlekoreaboy.putInt("sex", 1);
                singerActivityFragment.setArguments(bundlekoreaboy);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.koreagirl:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlekoreagirl=new Bundle();
                bundlekoreagirl.putString("name", "韩国女歌手");
                bundlekoreagirl.putInt("area", 7);
                bundlekoreagirl.putInt("sex", 2);
                singerActivityFragment.setArguments(bundlekoreagirl);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.korea:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlekorea=new Bundle();
                bundlekorea.putString("name", "韩国组合");
                bundlekorea.putInt("area", 7);
                bundlekorea.putInt("sex", 3);
                singerActivityFragment.setArguments(bundlekorea);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.japanboy:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlejapanboy=new Bundle();
                bundlejapanboy.putString("name", "日本男歌手");
                bundlejapanboy.putInt("area", 60);
                bundlejapanboy.putInt("sex", 1);
                singerActivityFragment.setArguments(bundlejapanboy);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.japangirl:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlejapangirl=new Bundle();
                bundlejapangirl.putString("name", "日本女歌手");
                bundlejapangirl.putInt("area", 60);
                bundlejapangirl.putInt("sex", 2);
                singerActivityFragment.setArguments(bundlejapangirl);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.japan:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundlejapan=new Bundle();
                bundlejapan.putString("name", "日本组合");
                bundlejapan.putInt("area", 60);
                bundlejapan.putInt("sex", 3);
                singerActivityFragment.setArguments(bundlejapan);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;
            case R.id.other:
                singerActivityFragment=new SingerActivityFragment();
                Bundle bundleother=new Bundle();
                bundleother.putString("name", "其他歌手");
                bundleother.putInt("area", 5);
                bundleother.putInt("sex", 0);
                singerActivityFragment.setArguments(bundleother);
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.container, singerActivityFragment).commit();
                break;

        }

    }


}
